﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;
using V5_Model;

namespace V5_DAL
{
    public class DALProductField
    {
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_ProductField");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ModelProductField model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_ProductField(");
            strSql.Append("FClassID,FieldName,FieldValues,FieldType,IsUseImg,FieldPics,Choice)");
            strSql.Append(" values (");
            strSql.Append("@FClassID,@FieldName,@FieldValues,@FieldType,@IsUseImg,@FieldPics,@Choice)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FClassID", SqlDbType.Int,4),
					new SqlParameter("@FieldName", SqlDbType.NVarChar,50),
					new SqlParameter("@FieldValues", SqlDbType.NVarChar,255),
					new SqlParameter("@FieldType", SqlDbType.Int,4),
					new SqlParameter("@IsUseImg", SqlDbType.Int,4),
					new SqlParameter("@FieldPics", SqlDbType.NVarChar,300),
					new SqlParameter("@Choice", SqlDbType.Int,4)};
            parameters[0].Value = model.FClassID;
            parameters[1].Value = model.FieldName;
            parameters[2].Value = model.FieldValues;
            parameters[3].Value = model.FieldType;
            parameters[4].Value = model.IsUseImg;
            parameters[5].Value = model.FieldPics;
            parameters[6].Value = model.Choice;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelProductField model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_ProductField set ");
            strSql.Append("FClassID=@FClassID,");
            strSql.Append("FieldName=@FieldName,");
            strSql.Append("FieldValues=@FieldValues,");
            strSql.Append("FieldType=@FieldType,");
            strSql.Append("IsUseImg=@IsUseImg,");
            strSql.Append("FieldPics=@FieldPics,");
            strSql.Append("Choice=@Choice");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@FClassID", SqlDbType.Int,4),
					new SqlParameter("@FieldName", SqlDbType.NVarChar,50),
					new SqlParameter("@FieldValues", SqlDbType.NVarChar,255),
					new SqlParameter("@FieldType", SqlDbType.Int,4),
					new SqlParameter("@IsUseImg", SqlDbType.Int,4),
					new SqlParameter("@FieldPics", SqlDbType.NVarChar,300),
					new SqlParameter("@Choice", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.FClassID;
            parameters[2].Value = model.FieldName;
            parameters[3].Value = model.FieldValues;
            parameters[4].Value = model.FieldType;
            parameters[5].Value = model.IsUseImg;
            parameters[6].Value = model.FieldPics;
            parameters[7].Value = model.Choice;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_ProductField ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelProductField GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,FClassID,FieldName,FieldValues,FieldType,IsUseImg,FieldPics,Choice from K_ProductField ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            ModelProductField model = new ModelProductField();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FClassID"].ToString() != "")
                {
                    model.FClassID = int.Parse(ds.Tables[0].Rows[0]["FClassID"].ToString());
                }
                model.FieldName = ds.Tables[0].Rows[0]["FieldName"].ToString();
                model.FieldValues = ds.Tables[0].Rows[0]["FieldValues"].ToString();
                if (ds.Tables[0].Rows[0]["FieldType"].ToString() != "")
                {
                    model.FieldType = int.Parse(ds.Tables[0].Rows[0]["FieldType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsUseImg"].ToString() != "")
                {
                    model.IsUseImg = int.Parse(ds.Tables[0].Rows[0]["IsUseImg"].ToString());
                }
                model.FieldPics = ds.Tables[0].Rows[0]["FieldPics"].ToString();
                if (ds.Tables[0].Rows[0]["Choice"].ToString() != "")
                {
                    model.Choice = int.Parse(ds.Tables[0].Rows[0]["Choice"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,FClassID,FieldName,FieldValues,FieldType,IsUseImg,FieldPics,Choice ");
            strSql.Append(" FROM K_ProductField ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,FClassID,FieldName,FieldValues,FieldType,IsUseImg,FieldPics,Choice ");
            strSql.Append(" FROM K_ProductField ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_ProductField";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }

        #endregion  成员方法
    }
}
